/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricsjob.h"

void LyricsJob::start() {
    // use the first provider to do the job
    m_cur = m_list.begin();
    m_p = (*m_cur)->create(m_track, this);
    connect(m_p, SIGNAL(signalSearched()), SLOT(slotJobSearched()));
    connect(m_p, SIGNAL(signalReady()), SLOT(slotJobDone()));
    m_status = LyricsProvider::Searching;
    m_p->search();
};

void LyricsJob::slotJobSearched() {
    if (m_p->resultSize() == 0) {
        // nothing found
        m_p->deleteLater();
        m_cur++;
        if (m_cur == m_list.end()) {
            // this is the last provider, we can conclude that there is no result
            m_status = LyricsProvider::NotFound;
            emit signalJobDone(this);
            return;
        } else {
            // otherwise we use the next provider
            m_p = (*m_cur)->create(m_track, this);
            connect(m_p, SIGNAL(signalSearched()), SLOT(slotJobSearched()));
            connect(m_p, SIGNAL(signalReady()), SLOT(slotJobDone()));
            m_p->search();
        };
    } else {
        // we found some result
        if (m_p->resultSize() == 1 && !m_prompt) {
            // only one result, we have no choice but to download it
            m_status = LyricsProvider::Downloading;
            m_p->acceptResult(0);
        } else {
            // there are more than one choice
            emit signalMultiChoice(this);
        };
    };
};

void LyricsJob::slotJobDone() {
    m_status = m_p->getStatus();
    if (m_status == LyricsProvider::Success) {
        m_filename = m_p->getFilename();
    } else {
        m_filename = QString("");;
    };
    emit signalJobDone(this);
};
